<template>
	<view class="content">
		<u-notice-bar v-if="flight_inquiry_off == 0" :is-circular='false' duration='2000'
			:list="['温馨提示：如有中转请因情况选择','航班号查询-接火车：系统会默认时间会延后15分钟','航班号查询-送火车：系统会默认会提前2小时']"></u-notice-bar>
		<u-notice-bar v-if="flight_inquiry_off == 1" :list="['提示：送火车请提前1-3小时']"></u-notice-bar>

		<view class="content_box">
			<template v-if="flight_inquiry_off == 0">

				<u-form labelWidth='23' labelPosition="left" :model="flight_inquiry_data">
					<u-form-item borderBottom leftIcon="level" :leftIconStyle="{'color': '#fa3534'}">
						<u-input v-model="flight_inquiry_data.num" :border="false" disabledColor="#ffffff"
							placeholder="请输入班次号, 如 K817"></u-input>
					</u-form-item>
					<u-form-item borderBottom leftIcon="calendar-fill" :leftIconStyle="{'color': '#ff9900'}">
						<u-input @click="data_btn" v-model="flight_inquiry_data.date" :border="false" disabled
							disabledColor="#ffffff" :placeholder="'请选择日期'  "></u-input>
					</u-form-item>
				</u-form>
				<view
					style="margin-top: 40rpx;margin-bottom: 30rpx;display: flex;padding: 0 20rpx;justify-content: space-evenly;">
					<view style="width: 250rpx;"><u-button @click="noflightnumber" type="success">无车次号</u-button></view>
					<view style="width: 250rpx;"><u-button @click="queryNow_btn" type="primary">立即查询</u-button>
					</view>
				</view>

			</template>
			<template v-if="flight_inquiry_off == 1">

				<u-form labelWidth='23' labelPosition="left" :model="flight_inquiry_data">
					<u-form-item borderBottom leftIcon="clock-fill" :leftIconStyle="{'color': '#2196f3'}">
						<view style="display: flex;">
							<u-input style="width: 150rpx;" @click="time_btn" v-model="noflight_inquiry_data.date"
								:border="false" disabled disabledColor="#ffffff" :placeholder="'请选择日期'  "></u-input>
							:
							<u-input maxlength='4' style="width: 450rpx;" type='number'
								v-model="noflight_inquiry_data.tiem" :border="false" disabledColor="#ffffff"
								:placeholder="'请输入时间,格式：1201为12:01' "></u-input>
						</view>
					</u-form-item>
				</u-form>
				<view style="color: red;margin-left: 27px;font-size: 25rpx;">{{prompt}}</view>
				<view
					style="margin-top: 40rpx;margin-bottom: 30rpx;display: flex;padding: 0 20rpx;justify-content: space-evenly;">
					<view style="width: 250rpx;"><u-button type="success">有航班号</u-button></view>
					<view style="width: 250rpx;"><u-button @click="ok_btn" type="primary">确认</u-button></view>
				</view>


			</template>

		</view>

		<view v-if="queryNow_list.length > 0 && flight_inquiry_off == 0" class="flight-list">

			<view class="no-service" v-if="isService">温馨提示：暂时只支持'成都'业务</view>
			<view style="height: 150rpx;">
				<view style="display: flex;justify-content: space-between;">
					<view>{{queryNow_data.trainno}}&nbsp;&nbsp;{{queryNow_data.typename}}</view>
					<view>耗时&nbsp;&nbsp;{{queryNow_data.costtimetxt?queryNow_data.costtimetxt:''}}</view>
				</view>
				<view style="display: flex;align-items: center;justify-content: center;margin-top: 20rpx;">
					<view style="margin-right: 20rpx;">{{queryNow_data.startstation}}</view>
					<u-icon name="/static/icons/arrow-right.png" width="70" height="24"></u-icon>
					<view style="margin-left: 20rpx;">{{queryNow_data.endstation}}</view>
				</view>
			</view>
			<view style="background-color: #adadad;width: 100%;height: 1px;"></view>
			<view style="height: 45vh;overflow: auto;">
				<view v-for="(item,index) in queryNow_list" :key="index">
					<view v-if="index == 0">
						<view style="display: flex;justify-content: space-between;height: 60rpx;align-items: center; ">
							<view>{{queryNow_list[0].station}} <text style="color: sandybrown;">(始发)</text></view>
							<view @click="fa_btn(queryNow_list[0])"
								style="width: 200rpx;background-color:#3C9CFF;text-align: center;border-radius: 12rpx;color: #fff;">
								{{queryNow_list[0].departuretime}}&nbsp;发车
							</view>

						</view>
						<view style="width: 100%;height: 1px;background-color: #F3F3F3;"></view>
					</view>
					<view v-if="index != 0 && index != (queryNow_list.length -1)">
						<view style="display: flex;justify-content: space-between;height: 60rpx;align-items: center; ">
							<view>{{queryNow_list[index].station}}</view>
							<view @click="jin_btn(queryNow_list[index])"
								style="padding: 5rpx 12rpx; background-color:#5AC725;text-align: center;border-radius: 12rpx;color: #fff;">
								{{queryNow_list[index].station.includes('成都')?'接 - ':""}}{{queryNow_list[index].arrivaltime}}&nbsp;进站
							</view>

						</view>
						<view style="display: flex;justify-content: space-between;height: 60rpx;align-items: center; ">
							<view>停{{time_fun(queryNow_list[index]) }}分钟 </view>

							<view @click="fa_btn(queryNow_list[index])"
								style="padding: 5rpx 12rpx;background-color:#3C9CFF;text-align: center;border-radius: 12rpx;color: #fff;">
								{{queryNow_list[index].station.includes('成都')?'送 - ':""}}{{queryNow_list[index].departuretime}}&nbsp;发车
							</view>
						</view>
						<view style="width: 100%;height: 1px;background-color: #F3F3F3;"></view>
					</view>
					<view v-if="index == (queryNow_list.length -1)">
						<view style="display: flex;justify-content: space-between;height: 60rpx;align-items: center; ">
							<view>{{queryNow_list[queryNow_list.length -1].station}} <text
									style="color: sandybrown;">(终点)</text></view>
							<view @click="jin_btn(queryNow_list[queryNow_list.length -1])"
								style="width: 200rpx;background-color:#5AC725;text-align: center;border-radius: 12rpx;color: #fff;">
								{{queryNow_list[queryNow_list.length -1].arrivaltime}}&nbsp;进站
							</view>

						</view>

					</view>
				</view>
			</view>

		</view>

		<!-- 弹窗 -->

		<template>

			<!-- 日期 -->
			<u-calendar @close="calendar_close" @change="calendar_confirm" :max-date="'2040-12-30'" :monthNum="12"
				color="#41A863" v-model="calendar_show"></u-calendar>
			<!-- 时间 -->
			<u-picker :params="params" @confirm="time_confirm" @cancel="time_cancel" v-model="time_show"
				:default-time="time_value" mode="time"></u-picker>
		</template>
	</view>
</template>

<script>
	export default {

		data() {
			return {
				flight_inquiry_off: '0',
				flight_inquiry_data: {
					num: "",
					date: "",
					travelWay: '2'
				},
				noflight_inquiry_data: {
					date: '',
					tiem: '',
				},
				calendar_show: false,
				isService: false,
				queryNow_list: [],
				queryNow_data: {},
				time_value: '',
				time_show: '',
				params: {
					year: true,
					month: true,
					day: true,
					// hour: true,
					// minute: true,
					// second: true
				},
				prompt: '',


			}
		},
		onReady() {
			let currentDate = new Date();

			let year = currentDate.getFullYear();
			let month = String(currentDate.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要加1，然后补0
			let day = String(currentDate.getDate()).padStart(2, '0');
			let hours = String(currentDate.getHours()).padStart(2, '0');
			let minutes = String(currentDate.getMinutes()).padStart(2, '0');
			let seconds = String(currentDate.getSeconds()).padStart(2, '0');

			// let formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
			let formattedDate = `${year}-${month}-${day}`;
			this.time_value = formattedDate



		},
		onLoad(e) {
			console.log(e)

			if (e.info) {
				this.flight_inquiry_data.num = e.info
			}
			if (e.date) {
				this.flight_inquiry_data.date = e.date
				this.queryNow_btn()
			}
		},
		methods: {
			time_fun(e) {
				let datatime1 = e.departuretime
				let datatime2 = e.arrivaltime
				let parts1 = datatime1.split(':')
				let parts2 = datatime2.split(':')
				let num1 = parseInt(parts1[0]) * 60 + parseInt(parts1[1])
				let num2 = parseInt(parts2[0]) * 60 + parseInt(parts2[1])
				let difference = Math.abs(num1 - num2);
				return difference
			},
			fa_btn(e) {
				if (e.station.includes('成都')) {
					let tabs_page = {
						tabs1: 3,
					}
					uni.setStorageSync('tabs_page', tabs_page);
					let data = {
						num: '火车',
						timedate: '',
						arrPort:'',
						depPort: e.station,
						flightNo: this.queryNow_data.trainno,
						depScheduled: this.flight_inquiry_data.date + ' ' + e.arrivaltime,
						arrScheduled: this.flight_inquiry_data.date + ' ' + e.departuretime,
						depTerminal: '',
						arrTerminal: '',
					}
					data.timedate = this.add2hours(this.flight_inquiry_data.date + ' ' + e.departuretime)
					
					uni.setStorageSync('aircraft', data);
					uni.reLaunch({
						url: '/pages/tabbar/order-list/order-list'
					});



				} else {
					return uni.$u.toast(e.station + '业务暂未开通');
				}
			},
			jin_btn(e) {
				if (e.station.includes('成都')) {
					let tabs_page = {
						tabs1: 3,
					}
					uni.setStorageSync('tabs_page', tabs_page);
					let data = {
						num: '火车',
						timedate: '',
						arrPort:  e.station,
						depPort:'',
						flightNo: this.queryNow_data.trainno,
						depScheduled: this.flight_inquiry_data.date + ' ' + e.arrivaltime,
						arrScheduled: this.flight_inquiry_data.date + ' ' + e.departuretime,
						depTerminal: '',
						arrTerminal: '',
					}
					data.timedate = this.add15Minutes(this.flight_inquiry_data.date + ' ' + e.arrivaltime)
					
					uni.setStorageSync('aircraft', data);
					uni.reLaunch({
						url: '/pages/tabbar/order-list/order-list'
					});


				} else {
					return uni.$u.toast(e.station + '业务暂未开通');
				}
			},
			noflightnumber(e) {
				this.flight_inquiry_off = 1
			},
			thereflightnumber(e) {
				this.flight_inquiry_off = 0
			},
			data_btn(e) {
				this.calendar_show = true
			},
			calendar_close(e) {
				this.calendar_show = false
			},
			calendar_confirm(e) {
				console.log(e)
				if (this.flight_inquiry_off == 0) {
					this.flight_inquiry_data.date = e.result
				} else {
					this.noflight_inquiry_data.date = e.result
				}
				this.calendar_show = false
			},
			queryNow_btn(e) {
				let that = this
				if (!that.flight_inquiry_data.num) {
					return uni.$u.toast('请输入火车号');
				} else if (!that.flight_inquiry_data.date) {
					return uni.$u.toast('请选择日期');
				}
				let ps = that.flight_inquiry_data
				that.$u.api.findTravelWayInfo(ps).then(res => {
					
					if(res.code == 0){
						return uni.$u.toast(res.msg);
					}else{
						that.queryNow_list = res.data.data.list
						that.queryNow_data = res.data.data
						that.isService = true
					}


				})
			},

			time_btn(e) {
				this.time_show = true
			},
			time_cancel(e) {
				this.time_show = false
			},
			time_confirm(e) {
				// console.log(e)

				// this.noflight_inquiry_data.date = e.year + "-" + e.month + "-" + e.day + " " + e.hour + ":" + e.minute;
				// this.noflight_inquiry_data.tiem = new Date(`${this.noflight_inquiry_data.date}:00`).getTime() // 将毫秒转换为秒

				// console.log(this.noflight_inquiry_data.tiem)
				// this.time_show = false
				this.noflight_inquiry_data.date = e.year + "-" + e.month + "-" + e.day
				console.log(this.noflight_inquiry_data.tiem)
				this.time_show = false
			},
			ok_btn(e) {
				let newtime = Date.now()
				if (this.noflight_inquiry_data.date == '') {
					return this.prompt = '请选择日期'
				} else {
					this.prompt = ''
				}
				if (this.noflight_inquiry_data.tiem == '') {
					return this.prompt = '请选择时间'
				} else {
					this.prompt = ''
				}
				if (this.noflight_inquiry_data.tiem.length != 4) {
					return this.prompt = '时间格式不对，列12:01请输入1201'
				} else if (this.noflight_inquiry_data.tiem[0] > 2) {
					return this.prompt = '时间格式不对，列12:01请输入1201'
				} else if (this.noflight_inquiry_data.tiem[0] == 2) {
					if (this.noflight_inquiry_data.tiem[1] > 4) {
						return this.prompt = '时间格式不对，列12:01请输入1201'
					} else {
						this.prompt = ''
					}

				} else {
					this.prompt = ''
				}
				let data_time = new Date(
					`${this.noflight_inquiry_data.date+' '+this.noflight_inquiry_data.tiem[0]+this.noflight_inquiry_data.tiem[1]+':'+this.noflight_inquiry_data.tiem[2]+this.noflight_inquiry_data.tiem[3]}:00`
				).getTime() // 将毫秒转换为秒
				if (newtime >= data_time) {
					return this.prompt = '不能小于当前时间'
				} else {
					this.prompt = ''

				}
				this.noflight_inquiry_data.date = this.noflight_inquiry_data.date + ' ' + this.noflight_inquiry_data.tiem[
						0] + this.noflight_inquiry_data.tiem[1] + ':' + this.noflight_inquiry_data.tiem[2] + this
					.noflight_inquiry_data.tiem[3]
				let tabs_page = {
					tabs1: 3, //跳转到添加订单
				}
				uni.setStorageSync('tabs_page', tabs_page);
				let ps = {
					num: '火车',
					timedate: this.noflight_inquiry_data.date,
					arrPort: '',
					depPort: '',
					flightNo: '',
					depScheduled: '',
					arrScheduled: '',
					depTerminal: '',
					arrTerminal: '',
				}
				uni.setStorageSync('aircraft', ps);
				uni.reLaunch({
					url: '/pages/tabbar/order-list/order-list'
				});


			},
			add30Minutes(dateTimeStr) {
				let date = new Date(dateTimeStr);
				date.setMinutes(date.getMinutes() + 30);

				let year = date.getFullYear();
				let month = (date.getMonth() + 1).toString().padStart(2, '0');
				let day = date.getDate().toString().padStart(2, '0');
				let hours = date.getHours().toString().padStart(2, '0');
				let minutes = date.getMinutes().toString().padStart(2, '0');

				let newDateTimeStr = `${year}-${month}-${day} ${hours}:${minutes}`;
				return newDateTimeStr;
			},
			add15Minutes(dateTimeStr) {
				let date = new Date(dateTimeStr);
				date.setMinutes(date.getMinutes() + 15);

				let year = date.getFullYear();
				let month = (date.getMonth() + 1).toString().padStart(2, '0');
				let day = date.getDate().toString().padStart(2, '0');
				let hours = date.getHours().toString().padStart(2, '0');
				let minutes = date.getMinutes().toString().padStart(2, '0');

				let newDateTimeStr = `${year}-${month}-${day} ${hours}:${minutes}`;
				return newDateTimeStr;
			},
			add4hours(dateString) {
				console.log(dateString);
				// 将时间字符串转换为日期对象
				let dateObj = new Date(dateString);

				// 将时间提前4个小时
				dateObj.setHours(dateObj.getHours() - 4);

				// 格式化新的日期时间
				let year = dateObj.getFullYear();
				let month = ("0" + (dateObj.getMonth() + 1)).slice(-2);
				let day = ("0" + dateObj.getDate()).slice(-2);
				let hours = ("0" + dateObj.getHours()).slice(-2);
				let minutes = ("0" + dateObj.getMinutes()).slice(-2);

				let newDateString = year + "-" + month + "-" + day + " " + hours + ":" + minutes;

				console.log(newDateString);
				// 输出新的日期时间字符串
				return newDateString;



			},
			add2hours(dateString) {
				console.log(dateString);
				// 将时间字符串转换为日期对象
				let dateObj = new Date(dateString);

				// 将时间提前4个小时
				dateObj.setHours(dateObj.getHours() - 2);

				// 格式化新的日期时间
				let year = dateObj.getFullYear();
				let month = ("0" + (dateObj.getMonth() + 1)).slice(-2);
				let day = ("0" + dateObj.getDate()).slice(-2);
				let hours = ("0" + dateObj.getHours()).slice(-2);
				let minutes = ("0" + dateObj.getMinutes()).slice(-2);

				let newDateString = year + "-" + month + "-" + day + " " + hours + ":" + minutes;

				console.log(newDateString);
				// 输出新的日期时间字符串
				return newDateString;



			},

		}
	}
</script>

<style lang="scss">
	.content_box {
		width: 90%;
		margin: auto;
		background-color: #fff;
		border-radius: 10rpx;
		padding: 10rpx 20rpx;
	}

	.u-form-item {
		padding: 10rpx 0 !important;
	}

	.flight-list {
		background: #fff;
		margin: 30rpx 20rpx;
		border-radius: 10rpx;
		padding: 10rpx;


		.no-service {
			border-bottom: 2rpx solid #efefef;
			padding: 10rpx;
			color: #ff9900;
			font-size: 28rpx;
		}

		.list-item {
			&:not(:last-child) {
				border-bottom: 2rpx solid #f3f3f3;
			}

			.li-main {
				display: flex;
				justify-content: space-between;
				padding: 30rpx 10rpx;

				.point-text {

					.time {
						font-size: 30rpx;
						font-weight: 600;
					}

					.port {
						font-size: 30rpx;
					}
				}
			}

			.bottom-info {
				display: flex;
				justify-content: space-between;
				margin-bottom: 10rpx;

				.flight-num {
					font-size: 30rpx;
					font-weight: 600;
					color: #666;
				}

				.flight-date {
					font-size: 30rpx;
					font-weight: 600;
					color: #666;
				}
			}

		}
	}
</style>